#ifndef __HTTPCONTEXT_H__
#define __HTTPCONTEXT_H__
#include "HttpRequest.h"

class MsgBuffer;

class HttpContext
{
  public:
    enum HttpRequestParseState
    {
        kExpectRequestLine,
        kExpectHeaders,
        kExpectBody,
        kGotAll,
    };

    HttpContext() : state_(kExpectRequestLine)
    {
    }

    // default copy-ctor, dtor and assignment are fine
    HttpContext(const HttpContext&) = default;
    HttpContext& operator=(const HttpContext&) = default;
    ~HttpContext() = default;

    // return false if any error
    bool parseRequest(MsgBuffer* buf, TimePoint receiveTime);

    bool gotAll() const
    {
        return state_ == kGotAll;
    }

    void reset()
    {
        state_ = kExpectRequestLine;
        HttpRequest dummy;
        request_.swap(dummy);
    }

    const HttpRequest& request() const
    {
        return request_;
    }

    HttpRequest& request()
    {
        return request_;
    }

  private:
    bool processRequestLine(const char* begin, const char* end);

    HttpRequestParseState state_;
    HttpRequest request_;
};

#endif  // __HTTPCONTEXT_H__